package projet.ejb;

import java.util.Collection;
import java.util.Date;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import projet.entity.Cours;
import projet.entity.Startup;

@Stateless
public class CoursFacadeEJB implements CoursFacade {
	@PersistenceContext(unitName = "SampleUnit")
	protected EntityManager entityMgr;
	
	public Cours add(Cours c) {
		entityMgr.persist(c);
		return c;
		
	}
	
	public Cours findByStartupAndDate(Startup startup,Date date){
		Query query = entityMgr.createQuery("SELECT c FROM Cours as c WHERE (c.startup=:s and c.date=:d) ");
		query.setParameter("s", startup);
		query.setParameter("d", date);
		Cours cours =  (Cours) query.getSingleResult();
		return cours;
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Cours> findAll() {
		 return entityMgr.createQuery("SELECT b FROM Cours as b").getResultList();
	}

	public void deleteAll() {
		Collection<Cours> l_cours = findAll();
		for(Cours c : l_cours){
			entityMgr.remove(c);
		}
	}

	public Cours update(Startup s,Date d, float v){
		Cours c=findByStartupAndDate(s,d);
		c.setValeur(v);
		return c;
	}	
}
